﻿using Newtonsoft.Json.Linq;
using ReportMealManagementSystem.Business;
using ReportMealManagementSystem.Entity;
using ReportMealManagementSystem.Tool;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Configuration;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;

namespace ReportMealManagementSystem
{
    /// <summary>
    /// MealUserStoredValue.xaml 的交互逻辑
    /// </summary>
    public partial class MealUserStoredValue : UserControl
    {
        Configuration cfa = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
        int districtID = int.Parse(ConfigurationManager.AppSettings["districtID"]);
        BasicInfo basicInfo = new BasicInfo();
        int pageIndex = 1;
        int pageSize = 50;
        int totalNumber = 0;
        int totalPage = 0;
        ObservableCollection<object> observableCollection = null;
        JArray jaData = new JArray();
        State state = State.None;
        JObject joItem = new JObject();
        JObject joParam = new JObject();
        ValueVerify valueVerify = new ValueVerify();

        public MealUserStoredValue()
        {
            InitializeComponent();
        }

        private void GetMealUserInfo()
        {
            string msg = "";
            JObject joMealUserParam = new JObject();
            joMealUserParam["PageIndex"] = 1;
            joMealUserParam["PageSize"] = 10000;
            joMealUserParam["DistrictID"] = districtID;
            int totalNumber = 0;
            int totalPage = 0;
            jaData = basicInfo.GetData(joMealUserParam, "gbt/UserVerify/UserListInfo", "用户", ref totalNumber, ref totalPage, ref msg);
            if (jaData != null)
            {
                JObject joItem = new JObject();
                List<SelectedItem> selectedItems = new List<SelectedItem>();
                selectedItems.Add(new SelectedItem
                {
                    ID = null,
                    Name = ""
                });
                for (int index = 0; index < jaData.Count; index++)
                {
                    joItem = (JObject)jaData[index];
                    selectedItems.Add(new SelectedItem
                    {
                        ID = (int)jaData[index]["userID"],
                        Name = (string)jaData[index]["number"] + "(" + (string)jaData[index]["name"] + ")"
                    });
                }

                cbUserName.Dispatcher.Invoke(new Action(delegate
                {
                    cbUserName.ItemsSource = selectedItems;
                    cbUserName.SelectedIndex = 0;
                }));
            }
        }

        private void GetMealUserStoreValue(JObject joParam)
        {
            string msg = "";
            var DataList = basicInfo.GetData(joParam, "gbt/UserVerify/UserListInfo", "账户余额明细", ref totalNumber, ref totalPage, ref msg);
            if (DataList != null)
            {
                JObject joItem = new JObject();
                observableCollection = new ObservableCollection<object>();
                for (int index = 0; index < DataList.Count; index++)
                {
                    joItem = (JObject)DataList[index];
                    observableCollection.Add(new
                    {
                        UserID = (int)joItem["userID"],
                        Number = (string)joItem["number"],
                        Name = (string)joItem["name"],
                        NickName = (string)joItem["nickName"],
                        Mobile = (string)joItem["mobile"],
                        Value = (string)joItem["value"],
                        IsAdmin = (bool)joItem["isAdmin"],
                        IsManager = (bool)joItem["isManager"],
                        IsTemporary = (bool)joItem["isTemporary"],
                        IsFinace = (bool)joItem["isFinace"],
                        DistrictNumber = (string)joItem["districtNumber"],
                        DistrictName = (string)joItem["districtName"],
                    });
                }

                lvDate.Dispatcher.Invoke(new Action(delegate
                {
                    lvDate.ItemsSource = observableCollection;
                    message.Content = msg;
                }));
            }
        }

        private void UserControl_Loaded(object sender, RoutedEventArgs e)
        {
            _loading.Visibility = Visibility.Visible;
            joParam = new JObject();
            joParam["DistrictID"] = districtID;
            joParam["PageIndex"] = pageIndex;
            joParam["PageSize"] = pageSize;
            Task task = new Task(new Action(delegate
            {
                GetMealUserInfo();
                GetMealUserStoreValue(joParam);
            }));
            task.Start();
            task.ContinueWith((t) =>
            {
                _loading.Dispatcher.Invoke(new Action(delegate
                {
                    _loading.Visibility = Visibility.Collapsed;
                }));
            });
        }

        private void Search_Click(object sender, RoutedEventArgs e)
        {
            if (state == State.None)
            {
                bData.Visibility = Visibility.Visible;
                lvDate.IsEnabled = false;
                state = State.Search;
                lblTitle.Content = "查询";
            }
            else if (state == State.Search)
            {
                _loading.Visibility = Visibility.Visible;
                bData.Visibility = Visibility.Collapsed;
                lvDate.IsEnabled = true;
                int UserID = 0;
                txtPageIndex.Text = "1";
                if (cbUserName.SelectedValue != null)
                {
                    UserID = (int)cbUserName.SelectedValue;
                }
                Task task = new Task(new Action(delegate
                {
                    pageIndex = 1;
                    joParam = new JObject();
                    joParam["PageIndex"] = pageIndex;
                    joParam["PageSize"] = pageSize;
                    if (UserID != 0)
                    {
                        joParam["UserID"] = UserID;
                    }

                    joParam["DistrictID"] = districtID;
                    GetMealUserStoreValue(joParam);
                }));
                task.Start();
                task.ContinueWith((t) =>
                {
                    _loading.Dispatcher.Invoke(new Action(delegate
                    {
                        _loading.Visibility = Visibility.Collapsed;
                        lvDate.IsEnabled = true;
                    }));
                    state = State.None;
                });
            }
        }

        private void Cancel_Click(object sender, RoutedEventArgs e)
        {
            state = State.None;
            bData.Visibility = Visibility.Collapsed;
            lvDate.IsEnabled = true;

            cbUserName.SelectedValue = null;
        }

        private void BtnClose_Click(object sender, RoutedEventArgs e)
        {
            state = State.None;
            bData.Visibility = Visibility.Collapsed;
            lvDate.IsEnabled = true;

            cbUserName.SelectedValue = null;
        }

        private void btnFirstPage_Click(object sender, RoutedEventArgs e)
        {
            _loading.Visibility = Visibility.Visible;
            txtPageIndex.Text = "1";
            Task task = new Task(new Action(delegate
            {
                pageIndex = 1;
                joParam["PageIndex"] = pageIndex;
                GetMealUserStoreValue(joParam);
            }));
            task.Start();
            task.ContinueWith((t) =>
            {
                _loading.Dispatcher.Invoke(new Action(delegate
                {
                    _loading.Visibility = Visibility.Collapsed;
                }));
            });
        }

        private void btnPageUp_Click(object sender, RoutedEventArgs e)
        {
            _loading.Visibility = Visibility.Visible;
            Task task = new Task(new Action(delegate
            {
                if (pageIndex > 1)
                {
                    pageIndex--;
                    txtPageIndex.Dispatcher.Invoke(new Action(delegate
                    {
                        txtPageIndex.Text = pageIndex.ToString();
                    }));
                    joParam["PageIndex"] = pageIndex;
                    GetMealUserStoreValue(joParam);
                }
            }));
            task.Start();
            task.ContinueWith((t) =>
            {
                _loading.Dispatcher.Invoke(new Action(delegate
                {
                    _loading.Visibility = Visibility.Collapsed;
                }));
            });
        }

        private void btnSkip_Click(object sender, RoutedEventArgs e)
        {
            _loading.Visibility = Visibility.Visible;
            if (!string.IsNullOrEmpty(txtPageIndex.Text.Trim()) && valueVerify.IsNumber(txtPageIndex.Text.Trim()))
            {
                pageIndex = int.Parse(txtPageIndex.Text.Trim());
                if (pageIndex == 0)
                {
                    pageIndex = 1;
                    txtPageIndex.Text = "1";
                }
                else if (pageIndex == totalPage)
                {
                    pageIndex = totalPage;
                    txtPageIndex.Text = totalPage.ToString();
                }
            }
            Task task = new Task(new Action(delegate
            {
                joParam["PageIndex"] = pageIndex;
                GetMealUserStoreValue(joParam);
            }));
            task.Start();
            task.ContinueWith((t) =>
            {
                _loading.Dispatcher.Invoke(new Action(delegate
                {
                    _loading.Visibility = Visibility.Collapsed;
                }));
            });
        }

        private void btnNextPage_Click(object sender, RoutedEventArgs e)
        {
            _loading.Visibility = Visibility.Visible;
            Task task = new Task(new Action(delegate
            {
                if (pageIndex < totalPage)
                {
                    pageIndex++;
                    txtPageIndex.Dispatcher.Invoke(new Action(delegate
                    {
                        txtPageIndex.Text = pageIndex.ToString();
                    }));
                    joParam["PageIndex"] = pageIndex;
                    GetMealUserStoreValue(joParam);
                }
            }));
            task.Start();
            task.ContinueWith((t) =>
            {
                _loading.Dispatcher.Invoke(new Action(delegate
                {
                    _loading.Visibility = Visibility.Collapsed;
                }));
            });
        }

        private void btnEndPage_Click(object sender, RoutedEventArgs e)
        {
            _loading.Visibility = Visibility.Visible;
            Task task = new Task(new Action(delegate
            {
                if (totalPage > 0)
                {
                    pageIndex = totalPage;
                }
                else
                {
                    pageIndex = 1;
                }
                txtPageIndex.Dispatcher.Invoke(new Action(delegate
                {
                    txtPageIndex.Text = pageIndex.ToString();
                }));
                joParam["PageIndex"] = pageIndex;
                GetMealUserStoreValue(joParam);
            }));
            task.Start();
            task.ContinueWith((t) =>
            {
                _loading.Dispatcher.Invoke(new Action(delegate
                {
                    _loading.Visibility = Visibility.Collapsed;
                }));
            });
        }
    }
}
